﻿using Hrm.Core;
using Hrm.Services;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Hrm.Web.Studio.Controllers
{
    public class HomeController : Controller
    {
        IServiceService _serviceService;
        IPortfolioService _portfolioService;
        IBlogService _blogService;
        ITeamMemberService _teamMemberService;
        IDictionaryService _dictionaryService;
        IEmailService _emailService;
        ICacheManager _cacheManager;
        public HomeController(IServiceService serviceService, IPortfolioService portfolioService, IBlogService blogService, ITeamMemberService teamMemberService, IDictionaryService dictionaryService, IEmailService emailService, ICacheManager cacheManager)
        {
            this._serviceService = serviceService;
            this._portfolioService = portfolioService;
            this._blogService = blogService;
            this._teamMemberService = teamMemberService;
            this._dictionaryService = dictionaryService;
            this._emailService = emailService;
            this._cacheManager = cacheManager;
        }

        public ActionResult Index()
        {
            ViewBag.Services = this._cacheManager.Get<List<Service>>(string.Format(Constant.HomePage, "TopServices3"), delegate() { return this._serviceService.TopServices(3); });
            ViewBag.Portfolios = this._cacheManager.Get<List<PortfolioDto>>(string.Format(Constant.HomePage, "TopPortfolios3"), delegate() { return this._portfolioService.TopPortfolios(3); });
            ViewBag.Blogs = this._cacheManager.Get<List<BlogPost>>(string.Format(Constant.HomePage, "TopBlogPostList4"), delegate() { return this._blogService.TopBlogPostList(4); });
            ViewBag.ClientTeamMenber = this._cacheManager.Get<List<TeamMenber>>(string.Format(Constant.HomePage, "AllClient"), delegate() { return this._teamMemberService.AllClient(); });
            return View();
        }

        public ActionResult Service()
        {
            var list = this._serviceService.GetList(p => p.IsEnable);
            ViewBag.PriceService = this._serviceService.GetAllPriceType();
            return View(list);
        }

        public ActionResult Portfolio()
        {
            var list = this._portfolioService.GetPortfolios();
            ViewBag.PortfolioTypes = this._portfolioService.GetPortfolioTypes();
            return View(list);
        }

        public ActionResult PortfolioItem(int id)
        {
            var model = this._portfolioService.GetPortfolio(id);
            ViewBag.RelatedPortfolios = this._portfolioService.GetRelatedPortfolios(id);
            return View(model);
        }

        public ActionResult Blog(string tag = "")
        {
            PageRequest<BlogPost> request = new PageRequest<BlogPost>();
            request.filter = new BlogPost();
            request.filter.Tags = tag;
            request.start = 0;
            request.length = 10;
            var result = this._blogService.PageBlogPostList(request);
            ViewBag.recordsTotal = result.recordsTotal;
            ViewBag.tag = tag;
            return View(result.data);
        }

        public JsonResult BlogJson(PageRequest<BlogPost> request)
        {
            request.start = (request.start - 1) * request.length;
            var result = this._blogService.PageBlogPostList(request);
            var jsonResult = Newtonsoft.Json.JsonConvert.SerializeObject(result);
            return Json(jsonResult);
        }

        public ActionResult BlogItem(int id)
        {
            ViewBag.PreBlog = this._blogService.PreBlogPost(id);
            var result = this._blogService.GetById(id);
            ViewBag.NextBlog = this._blogService.NextBlogPost(id);
            return View(result);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult BlogItem(BlogComment model)
        {
            var blog = this._blogService.GetById(model.BlogPost.Id);
            if (!string.IsNullOrEmpty(model.CommentText))
            {
                if (blog.BlogComments.Any(p => p.CommentText == model.CommentText))
                {
                    ModelState.AddModelError("CommentText", "已经有相同的评价内容了！");
                }
                else
                {
                    model.BlogPost = blog;
                    this._blogService.AddComment(model);
                }
            }
            else
            {
                ModelState.AddModelError("CommentText", "评价内容不能为空！");
            }
            ViewBag.PreBlog = this._blogService.PreBlogPost(model.BlogPost.Id);
            ViewBag.NextBlog = this._blogService.NextBlogPost(model.BlogPost.Id);
            return View(blog);
        }

        public ActionResult About()
        {
            var firstModel = this._teamMemberService.FirstTeamMenber();
            ViewBag.SecondToNTeamMenber = this._teamMemberService.SecondToNTeamMenber();
            ViewBag.Clients = this._teamMemberService.AllClient();
            return View(firstModel ?? new TeamMenber());
        }

        public ActionResult Contact()
        {
            ViewBag.ContactInfos = this._dictionaryService.GetDictionaryByType(Constant.DictionaryContactType);
            return View();
        }

        public PartialViewResult EmailView()
        {
            Email model = new Email();
            return PartialView("_EmailView", model);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        public PartialViewResult EmailView(Email model)
        {
            if (ModelState.IsValid)
            {
                this._emailService.Insert(model);
                return PartialView("_EmailSuccessView", model);
            }
            return PartialView("_EmailView", model);
        }

        public ActionResult KindleBackgroudTest()
        {
            return View();
        }

        public PartialViewResult PartViewPopularBlogs()
        {
            var PopularBlogPostList = this._blogService.PopularBlogPostList();
            return PartialView("_PopuplarBlogs", PopularBlogPostList);
        }

        public PartialViewResult PartViewTagsCloud()
        {
            var BlogTags = this._blogService.GetAllTag();
            return PartialView("_TagsCloud", BlogTags);
        }

        [AllowAnonymous]
        public ActionResult Page403()
        {
            return View();
        }

        [AllowAnonymous]
        public ActionResult Page404()
        {
            return View();
        }

        [AllowAnonymous]
        public ActionResult Page500()
        {
            return View();
        }
    }
}